package com.jcgyl.export.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jcgyl.export.entity.Role;
import com.jcgyl.export.entity.RoleQueryModel;
import com.jcgyl.export.service.RoleService;
import com.jcgyl.export.util.entity.Result;

@RestController
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;

	// 获取所有的角色信息
	@PostMapping("/findAllRole")
	public Map<String, Object> doFindAllRole(@RequestBody RoleQueryModel roleFilter) {
		String roleName = roleFilter.getFilter().getRoleName();
		String parentName = roleFilter.getFilter().getParentName();
		System.out.println("接受客户前台角色信息" + ":" + roleName + ":" + parentName);
		int pageIndex = roleFilter.getPageIndex();
		int pageSize = roleFilter.getPageSize();
		System.out.println("pageIndex:" + pageIndex + "pageSize:" + pageSize);
		Map<String, Object> map = roleService.findAllRole(roleName, parentName, pageSize, pageIndex);
		return map;
	}

	@PostMapping("/findRoleZtree")
	public List<Role> doFindZtreeObject() {
		return roleService.findRoleZtree();
	}
	
	@PostMapping("/findRoleName")
	public List<Role> doFindRoleName() {
		return roleService.findRoleName();
	}

	// 添加或修改角色信息
	@RequestMapping("/addOrUpdateRole")
	public Result doAndrole(@RequestBody Role role) {
		if (role.getId() == null) {
			return roleService.addRole(role);
		} else {
			return roleService.updateRole(role);
		}
	}

	// 删除角色信息
	@RequestMapping("/deleteRole")
	public Result Delete(@RequestBody Role role) {
		int roleId = role.getId();
		System.out.println("删除客户的Id" + roleId);
		if (roleId <= 0) {
			return Result.build(1, "请重新选择");
		}
		return roleService.deleteRole(roleId);

	}
}
